require 'test/unit'
require 'watir'

include Watir
class TestSuite < Test::Unit::TestCase
  
  def setup
    @b ||= Watir::IE.new    
     # @b.speed = :fast
     # @b.maximize
     # @b.bring_to_front
  end
  
  def teardown
    #@b.close
  end
  
  def test_0001
    # Q. How to click on links revealed in CSS 'balloon'
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0001.html')
    @b.link(:href, "http://www.opensource.org/").fire_event("onmouseover") 
    @b.link(:text, "90kts.com").click 
  end
  
  def test_0002
    # Q. How do I deal with an & character in my xpath?
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0002.html')
    @b.cell(:xpath, "//th[contains(text(),'Header')]").flash
    @b.cell(:xpath, "//td[contains(text(),'Data &amp; Elements')]").flash 
  end
  
  def test_0003
    # Q. How to populate a text_field
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0003.html')
    @b.text_field(:name, 'f1').set('te')
    assert_equal('text_field', @b.text_field(:name, 'f1').value)
    #@b.text_field(:name, 'f1').value('t')
  end
  
  def test_0004
    # Q. How do I check if the browser is visible?
    if @b.visible then
      puts "Browser IS visible"
    else
      puts "Browser IS NOT visible"
    end
  end
  
  def test_0005
    # Q. How do I check if text exists in browser?
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0005.html')
    puts "Text 'watir' DOES exist" if @b.text.include? 'watir'
    puts "Text 'regex' DOES NOT exist" unless @b.text =~ /regex/
    puts "Text 'furry little animals' DOES exist" if @b.text.include? 'furry little animals'
    puts "Text 'furry animals' DOES NOT exist" unless @b.text.include? 'furry animals'
  end
  
  def test_0006
    # Q. How do I get text from a browser?
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0006.html')
    puts /(1\d+)/.match(@b.text)
  end
  
  def test_0007
    # Q. How do I terminate a browser using a pid?
    hWnd ||= Watir::IE::Process.process_id_from_hwnd @b.hwnd
    pid=" " * 32
    thread=Win32API.new("user32", "GetWindowThreadProcessId", 'IP', 'I').Call(hWnd,pid)
    puts pid.unpack("L")[0]    
    right_to_terminate_process = 1
    handle = Win32API.new('kernel32.dll', 'OpenProcess', 'lil', 'l').
    call(right_to_terminate_process, 0, hWnd)
    Win32API.new('kernel32.dll', 'TerminateProcess', 'll', 'l').call(handle, 0)
    # note: following line prevents this test failing
    @b ||= Watir::IE.new
  end
  
  def test_0008
    # Q. How do I display the inner text of a DIV tag?
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0008.html')
    @b.elements_by_xpath("//div[@class='UserServiceValues']").each  do |elem|
      puts elem.innerText
    end
  end
  
  def test_0009
    @b.goto('http://justaddwatir.com/watir/test_html/tc_0001_0100/test_0009.html')
    puts @b.div(:xpath, "//div[@class='UserServiceValues']").innerText
    puts @b.div(:xpath, "//div[@class='UserServiceValues'][2]").innerText
  end
  
  def test_0010
    # Q. How do I detect if Internet Explorer is already running inside a watir script?
    if %x[tasklist /FI "IMAGENAME eq IEXPLORE.EXE" /NH] =~ /IEXPLORE/ then
      puts "browser IS running"
    else
      puts "no browsers running"
    end
  end
  
  def test_0011
    # Q. How do I open multiple Internet Explorer processes?
    require 'watir/contrib/ie-new-process'
    ies = []
    2.times do
      ie = Watir::IE.new_process
      ies<<ie
      ie.close
    end
  end
  
  def test_0012
    # Q. How do I close all other browsers except my current process?
    require 'watir/contrib/ie-new-process'
    ies = []
    2.times do
      ie = Watir::IE.new_process
      ies<<ie
    end
    @b.close_others
    @b.close # now close self
    # note: following line prevents this test failing
    #@b ||= Watir::IE.new
  end

end
